package com.github.aloxc.plugin.restplus.component;

import com.github.aloxc.plugin.restplus.entity.SettingType;
import com.github.aloxc.plugin.restplus.i18n.I18Bundle;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.IconLoader;
import com.intellij.ui.components.JBPanel;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;

import javax.swing.*;
import java.awt.*;
/**
 *
 * @author leero
 */
public class RequestSetting extends JBPanel implements ISettingPanel {
    private JPanel rootPanel;
    private JPanel settingPane;
    private Dialog dialog;

    private Variate variate;
    private Project project;
    private String url;
    private String parentName;

    @Override
    protected void printComponent(Graphics g) {
        super.printComponent(g);
    }

    public RequestSetting(Project project,String url,String parentName){
        super();
        this.url = url;
        this.project = project;
        this.parentName = parentName;
        initComponent();
    }

    private void initComponent() {
        initUi();
    }


    private void initUi() {
        GridLayoutManager settingLayoutManager = new GridLayoutManager(1, 1);
        settingLayoutManager.setHGap(1);
        settingLayoutManager.setVGap(1);
        settingLayoutManager.setMargin(new Insets(0,20,50,20));

        settingPane = new JBPanel<>();
        settingPane.setLayout(settingLayoutManager);

        this.setBorder(BorderFactory.createEmptyBorder());
        this.setLayout(new GridLayoutManager(1, 1));

        initPanel();
    }

    private void initPanel() {
        int height = 640;
        int width = 840;
        this.setMaximumSize(new Dimension(width,height));
        this.setMinimumSize(new Dimension(width,height));
        this.setPreferredSize(new Dimension(width,height));

        com.github.aloxc.plugin.restplus.component.Dialog dialog = new Dialog(null, this,
                new Dimension(width,height),
                new Dimension(width,height),
                new Dimension(width,height),
                IconLoader.getIcon("/icons/setting.png"),
                "Request variate(s) ("+ url +")"
        );
        this.dialog = dialog;


        //TODO 处理设置请求的环境，使用参数包含url的Variate构造函数
        variate = new Variate(SettingType.REQUEST,this.url,project,parentName,dialog);
        settingPane.add(variate,
                new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_SOUTHEAST, GridConstraints.FILL_BOTH,
                        GridConstraints.SIZEPOLICY_FIXED,
                        GridConstraints.FILL_VERTICAL | GridConstraints.SIZEPOLICY_CAN_GROW,
                        new Dimension(-1,-1), new Dimension(-1,-1), new Dimension(-1,-1)));
        Dimension size460 = new Dimension(-1, 640);
        this.add(settingPane,
                new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_SOUTHEAST, GridConstraints.FILL_BOTH,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                        GridConstraints.FILL_NONE | GridConstraints.FILL_NONE,
                        size460, size460, size460));
    }

    @Override
    public void setDialog(Dialog dialog) {
        this.dialog = dialog;
        variate.setSettingType(SettingType.REQUEST);
        this.variate.setDialog(dialog);
    }

    public Dialog getDialog() {
        return dialog;
    }

    @Override
    public void setProject(Project project) {
        this.project = project;
    }
}
